package com.acwing.partition4;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/21 14:57
 */
public class AC320能量项链 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[] nums = new int[n];
        s = reader.readLine().split("\\s+");
        for (int i = 0; i < n; i++) nums[i] = Integer.parseInt(s[i]);
        writer.write(String.valueOf(dynamicProgramming(nums)));
        writer.flush();
    }

    private static int dynamicProgramming(int[] nums) {
        int n = nums.length, answer = 0;
        int[] energies = new int[n << 1];
        for (int i = 0; i < n; i++) energies[i] = energies[i + n] = nums[i];
        int[][] dp = new int[2 * n][2 * n];
        for (int length = 3; length <= n + 1; length++) {
            for (int i = 0; i + length - 1 < n << 1; i++) {
                int j = i + length - 1;
                for (int k = i + 1; k < j; k++) {
                    dp[i][j] = Math.max(dp[i][j], dp[i][k] + dp[k][j] + energies[i] * energies[k] * energies[j]);
                }
            }
        }
        for (int i = 0; i < n; i++) answer = Math.max(answer, dp[i][i + n]);
        return answer;
    }
}
